home *** CD-ROM | disk | FTP | other *** search
Wrap
<?xml version="1.0" encoding="utf-8"?> <!-- =========================================================== Category: NonXML Sub-category: FixedLengthText Author: David Silverlight HeadGeek@xmlpitstop.com Created: 2001-05-16 Description:- This stylsheet demonstrates transforming a fixed length text file as an HTML table. In this example we are using substring functions to create an HTML document from a text file based on the start and end positions of the data. Additionally, we are calling a template named TrimTrailing to remove any extra traling spaces from our table. ================================================================ --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" /> <xsl:template match="/"> <html> <head> <style type="text/css"><![CDATA[ H1 {COLOR: red; FONT-FAMILY: Arial; FONT-SIZE: 14pt;} H2 {COLOR: darkblue; FONT-FAMILY: Arial; FONT-SIZE: 12pt;} .head {COLOR: darkblue; FONT-FAMILY: Arial; FONT-SIZE: 14pt;} .subhead {COLOR: darkblue; FONT-FAMILY: Arial; FONT-SIZE: 12pt;} .text {COLOR: black; FONT-FAMILY: Arial; FONT-SIZE: 12pt;} TH {COLOR: white; FONT-FAMILY: Arial; background-color: darkblue;} TD {COLOR: darkblue; FONT-FAMILY: Arial} TR { background-color: beige; } BODY { background-color: beige; } ]]></style> </head> <body> <xsl:for-each select="*"> <xsl:variable name="allContents" select="." /> <!--In this template, I load the file into a variable so that it can be handled as a large string. Using the string functions like substring, it is easy to parse it given the startpositions and lengths of the elements --> <table border="1"> <tr> <th>ID</th> <th>Title</th> <th>Name</th> <th>Phone</th> </tr> <xsl:call-template name="SplitFixedLengthToHTML"> <xsl:with-param name="strInput" select="$allContents" /> <xsl:with-param name="lineLength" select="72" /> </xsl:call-template> </table> </xsl:for-each> </body> </html> </xsl:template> <xsl:template name="SplitFixedLengthToHTML"> <xsl:param name="strInput" select="''" /> <xsl:param name="lineLength" select="120" /> <!-- This template recursively calls itself with the input string each iteration process one line in the text file --> <xsl:choose> <xsl:when test="string-length($strInput) > $lineLength"> <tr> <td> <xsl:call-template name="TrimTrailing"> <xsl:with-param name="strInput" select="substring($strInput, 2, 10)" /> </xsl:call-template> </td> <td> <xsl:call-template name="TrimTrailing"> <xsl:with-param name="strInput" select="substring($strInput, 12, 25)" /> </xsl:call-template> </td> <td> <xsl:call-template name="TrimTrailing"> <xsl:with-param name="strInput" select="substring($strInput, 37, 21)" /> </xsl:call-template> </td> <td> <xsl:call-template name="TrimTrailing"> <xsl:with-param name="strInput" select="substring($strInput, 58, 14)" /> </xsl:call-template> </td> </tr> <xsl:call-template name="SplitFixedLengthToHTML"> <!-- Here, I am calling the template recursively with the same string less one line --> <xsl:with-param name="strInput" select="substring($strInput, $lineLength + 1)" /> <xsl:with-param name="lineLength" select="$lineLength" /> </xsl:call-template> </xsl:when> </xsl:choose> </xsl:template> <xsl:template name="TrimTrailing"> <!--This template will recursively trim the trailing spaces from a string--> <xsl:param name="strInput" select="''" /> <xsl:variable name="strLen" select="string-length($strInput)" /> <xsl:variable name="strOutput" select="substring($strInput, 1, $strLen - 1 )" /> <xsl:variable name="strLastChar" select="substring($strInput, $strLen, 1 )" /> <xsl:choose> <xsl:when test="$strLastChar = ' '"> <xsl:call-template name="TrimTrailing"> <xsl:with-param name="strInput" select="$strOutput" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$strInput" /> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>